using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace UnitTestWinRTConverters.ObjectsTestBase
{
    public abstract class UIObjectTestBase : INotifyPropertyChanged
    {
        public Binding BindingObject { get; set; }

        private object _sourceValue;
        public object SourceValue
        {
            get { return _sourceValue; }
            set 
            { 
                _sourceValue = value;
                OnPropertyChanged("SourceValue");
            }
        }
        
        public object ConverterParameter
        {
            set
            {
                if (BindingObject != null)
                {
                    BindingObject.ConverterParameter = value;
                }
            }
        }

        public abstract object ConvertValue { get; }

        public object ConvertBackValue
        {
            get { return SourceValue; }
        }

        protected UIObjectTestBase(IValueConverter converter, BindingMode mode, object sourceValue, object converterParameter = null)
        {
            SourceValue = sourceValue;
            BindingObject = new Binding
            {
                Converter = converter,
                ConverterParameter = converterParameter,
                Source = this,
                Path = new PropertyPath("SourceValue"),
                Mode = mode
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
